<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="120px">
      <el-form-item label="日期">
        <el-date-picker
          v-model="date"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="城市公司名称" prop="comName">
        <el-input
          v-model="queryParams.comName"
          placeholder="请输入城市公司名称"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 导出 -->
    <el-row :gutter="2" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['marketingStat:coupon:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" border :data="getCouponDetails">
      <el-table-column label="日期" width="150">
        <template slot-scope="scope">
          <span>{{ scope.row.date | showTxt }}</span>
        </template>
      </el-table-column>
      <el-table-column label="城市公司名称" width="200">
        <template slot-scope="scope">
          <span>{{ scope.row.comName  | showTxt }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发券量" width="150">
        <template slot-scope="scope">
          <span>{{ scope.row.sendAmount  | showTxt }}</span>
        </template>
      </el-table-column>
      <el-table-column label="领券客户量" width="150">
        <template slot-scope="scope">
          <span>{{ scope.row.receiveAmount | showTxt }}</span>
        </template>
      </el-table-column>
      <el-table-column label="核销量" width="150">
        <template slot-scope="scope">
          <span>{{ scope.row.hxAmount | showTxt }}</span>
        </template>
      </el-table-column>
      <el-table-column label="净发券量" width="150">
        <template slot-scope="scope">
          <span>{{ scope.row.netSendAmount | showTxt }}</span>
        </template>
      </el-table-column>
      <el-table-column label="净领券客户量" width="150">
        <template slot-scope="scope">
          <span>{{ scope.row.netReceiveAmount | showTxt }}</span>
        </template>
      </el-table-column>
      <el-table-column label="净核销量" >
        <template slot-scope="scope">
          <span>{{ scope.row.netHxAmount | showTxt }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
  import { reportList, reportExport } from "@/api/marketingStat/couponCount";

  export default {
    name: "couponCount",
    data() {
      return {
        loading: false,
        // 总条数
        total: 0,
        // 时间范围
        date: [],
        // 发券统计列表
        getCouponDetails: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          comName:undefined,  //城市公司名称
          sendAmount:undefined,  //发券量
          receiveAmount:undefined,  //领券客户量
          hxAmount:undefined,  //核销量
          netSendAmount:undefined,  //净发券量
          netReceiveAmount:undefined,  //净领券客户量
          netHxAmount:undefined  //净核销量
        }
      };
    },
    filters: {
      showTxt(txt) {
        return txt ? txt : '--'
      }
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询发券明细列表 */
      getList() {
        this.loading = true;
        if(this.date.length) {
          this.addCouponDateRange('date', this.queryParams, this.date)
        }

        reportList(this.queryParams).then(
          response => {
            this.getCouponDetails = response.rows;
            this.total = response.total;
            this.loading = false;
          }
        );
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.fqCreateTime = [];
        this.date = [];
        if (this.queryParams.dateBegin) {
          this.addCouponDateRange('date', this.queryParams, this.date)
        }
        this.resetForm("queryForm");
        this.handleQuery();
      },

      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有发券明细?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return reportExport(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
      }
    }
  };
</script>
